/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.template.freemarker;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.TemplateException;

/** 
 * @author LiBo/Alex
 * @version V1.0 
 */
public class FreemarkerStream<T> {
    
    
    private FreemarkerContext context;
    
    
    public FreemarkerContext getContext() {
        return context;
    }
    
    /*public FreemarkerStream() {
        super();
    }*/

    public FreemarkerStream(FreemarkerContext context) {
        super();
        this.context = context;
    }
    /**  
     * @param templateName
     * @param param
     * @return
     * @exception
     */ 
    public String print(String templateName,T param) {
        //string printer
        try(StringWriter writer = new StringWriter()) {
            FreemarkerTemplate.getTemplate(templateName,context).
            process(param, writer);
            return writer.toString();
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**  
     * @param templateName
     * @return
     * @exception
     */ 
    public String print(String templateName) {
        //string printer
        try(StringWriter writer = new StringWriter()) {
            FreemarkerTemplate.getTemplate(templateName,getContext()).
            process(new Object(), writer);
            return writer.toString();
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**  
     * @param templateName
     * @param param
     * @param filePath
     * @exception
     */ 
    public void writer(String templateName,T param,String filePath) {
        try(
             FileOutputStream fos = new FileOutputStream(filePath);
             OutputStreamWriter writer = new OutputStreamWriter(fos);
             ) {
            FreemarkerTemplate.getTemplate(templateName,getContext()).process(param, writer);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
    }
    
    /**  
     * @param templateName
     * @param filePath
     * @exception
     */ 
    public void writer(String templateName,String filePath) {
        try(
             FileOutputStream fos = new FileOutputStream(filePath);
             OutputStreamWriter writer = new OutputStreamWriter(fos);
             ) {
            FreemarkerTemplate.getTemplate(templateName,getContext()).
            process(new Object(), writer);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
    }
    
    
    /**  
     * @param templateName
     * @param param
     * @param filePath
     * @exception
     */ 
    public void writer(String templateName,T param,Writer writer) {
        try {
            FreemarkerTemplate.getTemplate(templateName,getContext()).
            process(param, writer);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
    }
    
    /**  
     * @param templateName
     * @param filePath
     * @exception
     */ 
    public void writer(String templateName,Writer writer) {
        try {
            FreemarkerTemplate.getTemplate(templateName,getContext()).
            process(new Object(), writer);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void main(String[] args) {
        FreemarkerStream<Map<String,Object>>printer = 
                new FreemarkerStream(new FreemarkerContext().
                        createContext("src/main/resources/META-INF/resources/"));
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("classPath", "aasasd");
        params.put("className", "aasasdclass");
        params.put("helloWorld", "aasasdclass");
        printer.writer("code.ftl",params, "C:\\test.java");
    }
    
}   
